package com.foreveross.taskservice.common.mq.converter;

import java.io.Serializable;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.ObjectMessage;
import javax.jms.Session;

import org.springframework.jms.support.converter.MessageConversionException;
import org.springframework.jms.support.converter.MessageConverter;

public class TaskMessageConverter implements MessageConverter{

	@Override
	public Object fromMessage(Message msg) throws JMSException,
			MessageConversionException {
		if(msg!=null && msg instanceof ObjectMessage ){
			ObjectMessage objMsg=(ObjectMessage)msg;
			return  objMsg.getObject();
		}
		return null;
	}

	@Override
	public Message toMessage(Object obj, Session session) throws JMSException,
			MessageConversionException {
		if(obj instanceof Serializable){
			return session.createObjectMessage((Serializable) obj);
		}else{
			Message msg=session.createMessage();
			msg.setObjectProperty("OBJ", obj);
			return msg;
		}
		
	}

}
